var TIMEREQUEST = 900;
var roomsname = new Array();
var roomsid = new Array();
var roomsrow = new Array();
var roomstext = new Array();
var readeds = new Array();
var roomtyping = new Array();
var tout = 0;
var roomnow =- 1;
var idnow = 0;
var roomcount = 0;
var loaded = 0;
var username = "";
var textsend = "";
var lengthtemp = 0;
var idban =- 1;
var hasSendEmail=0;
function SendMessage()
{
    if (idnow <= 0) {
        return false
    }
    textsend += idnow + "|" + $("input_text").value + "||";
    $("input_text").value = ""
}
function AutoCheck()
{
    post = "listguest=";
    for (u = 0; u < roomcount; u++) {
        post += roomsid[u] + "|" + roomsrow[roomsid[u]] + "||"
    }
    post += "&ms=" + textsend;
    post += "&length=" + lengthtemp;
    post += "&idban=" + idban;
    post += "&idnow=" + idnow;
    if (($("input_text").value).length > 0) {
        post += "&typing=1"
    }
	if(hasSendEmail==1)
	{
		post +="&SendEmail=1";
		alert("Sent all content chat to your email!");
	}
	hasSendEmail=0;
    textsend = "";
    idban =- 1;
    ajaxLoader("chat.php?opt=check", post, "", "densang", "ProAutoCheck")
}
function ProAutoCheck(str)
{
    try
    {
        if (str == "out") {
            alert("orther person logined");
            location.href = "chat.php?opt=login"
        }
        else if (str == "timeout") {
            alert("You Logouted by timeout");
            location.href = "chat.php?opt=login"
        }
        $("listonline").innerHTML = "";
        roomcount = 0;
        arr = str.split("(*_*)");
        for (i = 1; i < arr.length - 1; i++)
        {
            arr2 = arr[i].split("||");
            BuildData(arr2[0], arr2[1], arr2[2], arr2[3], arr2[4], arr2[5])
        }
        loaded = 1;
        if (str == ".(*_*)") {
            $("displaydiv").innerHTML = "";
            $("listonline").innerHTML = 'No Guest Online';
        }
    }
    catch (e) {
        window.status = e
    }
    DisplayText();
    tout = setTimeout("AutoCheck()", TIMEREQUEST)
}
function BuildData(name, id, status, text, typing, time)
{
    $("listonline").innerHTML += "<div id='on_" + id + "' onclick='DisplayText(" + id + ")' class=online align=left><img src='images/psn.gif'>&nbsp;&nbsp;" + name + "<div style='float:right; ' id='new_" + id + "'></div></div><div class='close' onclick='bannedid(" + id + ")'></div>";
    if (readeds[id] == 0 && idnow != id) {
        $("new_" + id).innerHTML += "<img src='images/newms.gif'>"
    }
    if (idnow == 0) {
        idnow = id
    }
    rowc = 0;
    html = "";
    arrb = text.split("$$");
    for (j = 0; j < arrb.length; j++)
    {
        arr2b = arrb[j].split("++");
        if (arr2b[1] == undefined) {
            continue
        }
        cuser = "user2";
        ctext = "text2";
        if (username == arr2b[0]) {
            cuser = "user1";
            ctext = "text1"
        }
        html += "&nbsp;<span class=" + cuser + ">" + arr2b[0] + ":</span> <span class=" + ctext + ">" + arr2b[1] + "</span><br>";
        rowc++;
        window.focus();
        readeds[id] = 0
    }
    if (roomsrow[id] == undefined) {
        roomsrow[id] = 0
    }
    if (roomstext[id] == undefined) {
        roomstext[id] = ""
    }
    roomsid[roomcount] = id;
    roomsname[id] = name;
    roomtyping[id] = typing;
    roomstext[id] += html;
    roomsrow[id] += rowc;
    roomcount++
}
function DisplayText(id)
{
    try {
        $("on_" + idnow).className = "online"
    }
    catch (e) {
        idnow = roomsid[0];
        window.status = e
    }
    try
    {
        $("on_" + idnow).className = "online";
        if (id != undefined) {
            idnow = id
        }
        textsh = (roomstext[idnow] != undefined) ? roomstext[idnow] : "connected";
        if (roomtyping[idnow] == 1) {
            textsh += "<div class='typing'> <b>" + roomsname[idnow] + "</b> typing...</div>"
        }
        $("displaydiv").innerHTML = textsh;
        $("displaydiv").scrollTop = 19999;
        $("on_" + idnow).className = "onnow";
        readeds[idnow] = 1
    }
    catch (e) {
        window.status = e;
    }
}
function bannedid(id)
{
    idban = id;
    $("on_" + id).innerHTML = ''
}
